<template>
    <div>
        <a-card title="LNB">
            <a-button slot="extra" type='primary' style="margin-right: 10px;">重置</a-button>
            <a-button slot="extra" type='primary' @click="sendConfig('formRules')">设置</a-button>
            <a-form-model ref="formRules" :rules="rulesForm" :model="formLNB" :label-col="labelCol"
                :wrapper-col="wrapperCol">
                <a-row>
                    <a-col :span=24>
                        <a-form-model-item label="设备地址" prop="deviceAddress">
                            <a-input placeholder="请输入一个16进制数" style="width:99%"
                                v-model="formLNB.deviceAddress"></a-input>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span=24>
                        <a-form-model-item label="输入频率(GHz)">
                            <a-input-number style="width:100%" v-model="formLNB.inputFrequency"></a-input-number>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span=24>
                        <a-form-model-item label="输出频率(GHz)">
                            <a-input-number style="width:100%" v-model="formLNB.outputFrequency"></a-input-number>
                        </a-form-model-item>
                    </a-col>
                </a-row>


            </a-form-model>
        </a-card>
    </div>


</template>

<script>
import { httpAction } from '@/api/manage.js'
export default {
    data() {
        return {
            formLNB: {},
            labelCol: { span: 10 },
            wrapperCol: { span: 14 },
            url: {
                sendDevicePackage: '/device/nmcDeviceGridValue/sendDevicePackage'
            },
            rulesForm: {
                deviceAddress: [{ required: false, message: '请输入', trigger: 'blur' }, { validator: this.validateDeviceAddress }],
            }

        }
    },
    methods: {
        validateDeviceAddress(rule, value, callback) {
            let reg = /^[A-Fa-f0-9]{1,2}$/
            if (!reg.test(value)) {
                callback('请输入十六进制数')
            } else {
                callback()
            }
        },
        sendConfig(formRules) {
            this.$refs[formRules].validate((valid) => {
                if (valid) {
                    let temp = {
                        "channelCode": "LNB",
                        "gridValues": [
                            {
                                "gridCode": "LNB-设置地址-地址",
                                "gridValue": this.formLNB.deviceAddress


                            },
                        ],
                        "packageCode": "LNB-设置地址"
                    }
                    httpAction(this.url.sendDevicePackage, temp, 'post').then(res => {
                        if (res.success) {
                            this.$message.success(res.message)
                            let data = {
                                "channelCode": "LNB",
                                "gridValues": [
                                    {
                                        "gridCode": "LNB-设备控制-输入频率",
                                        "gridValue": Number((this.formLNB.inputFrequency * 1000000000) / 100).toString(16)


                                    },
                                    {
                                        "gridCode": "LNB-设备控制-输出频率",
                                        "gridValue": Number((this.formLNB.outputFrequency * 1000000000) / 100).toString(16)
                                    },
                                ],
                                "packageCode": "LNB-设备控制"
                            }
                            httpAction(this.url.sendDevicePackage, data, 'post').then(res => {
                                if (res.success) {
                                    this.$message.success(res.message)
                                } else {
                                    this.$message.error(res.message)
                                }
                            })
                        } else {
                            this.$message.error(res.message)
                        }
                    })




                } else {
                    console.log('error submit!!')
                    return false
                }
            })
            // this.$refs[formRules].validateFields(err => {
            //     if (!err) {
            // }
            // }
            // );

        },
    }
}
</script>

<style scoped>
.ant-card-bordered {
    height: 330px;
    overflow-y: auto;
}
</style>